﻿using HandyControl.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Threading;
using WpfAGVSystem.Common;
using WpfAGVSystem.DBModel;
using WpfAGVSystem.InterFace;

namespace WpfAGVSystem.ViewModels
{

    public class QueueModelFormViewModel : BindableBase, INavigationAware
    {
        public readonly IEventAggregator eventAggregator;
        private QueueClass Queue;
        private IDBInterface dB;
        public DelegateCommand<string> OperationCommand { get; private set; }
        public DelegateCommand DisplayCommand { get; private set; }
        public ObservableCollection<QueueModel_Entity> DataList { get; set; } = new ObservableCollection<QueueModel_Entity>();
        public ObservableCollection<QueueModel_Entity> DataListDisplay { get; set; } = new ObservableCollection<QueueModel_Entity>();
        public int Count;
        public int Index { get; set; }
        public int PangCount { get; set; } = 15;
        private bool Flag { get; set; }
        public int Pages { get; set; } = 0;
        private bool caninit(string name)
        {
            return Flag;
        }
        public  QueueModelFormViewModel(IDBInterface dBInterface, QueueClass queueClass, IEventAggregator _eventAggregator)
        {
            eventAggregator = _eventAggregator;
            Queue = queueClass;
            Flag = true;
            OperationCommand = new DelegateCommand<string>(Operation, caninit).ObservesProperty(() => Flag);
            DisplayCommand = new DelegateCommand(() => {
                DataListDisplay = new ObservableCollection<QueueModel_Entity>(DataList.Skip((Index - 1) * PangCount).Take(PangCount));
            });
            dB = dBInterface;
        }
        private void init()
        {
          
            ReloadQueue();
     
        }
        private void Operation(string str)
        {
            Flag = false;
            switch (str)
            {
                case "resh":
                    RefresshQueue();
                    break;
                case "clear":
                    ClearQueue();
                    break;
                case "test":
                    Queue.Enqueue(1,1);
                    break;
                default:
                    break;
            }
        
        }

        private void ReloadQueue()
        {

        ;
            Index = 1;
            var list= dB.Where<QueueModel_Entity>().ToList();
            Count = list.Count();
            Pages = Count / PangCount;
            DataList = new ObservableCollection<QueueModel_Entity>(list);

                App.Current.Dispatcher.Invoke(() =>
                {
                    DataListDisplay = new ObservableCollection<QueueModel_Entity>(DataList.Skip((Index - 1) * PangCount).Take(PangCount));

                }, DispatcherPriority.Background);

         





        }
        public void RefresshQueue()
        {
            Task.Run(() => {
                Queue.Save2Db();
                ReloadQueue();
                Flag = true;
            });


        }
        public void ClearQueue()
        {
            eventAggregator.GetEvent<LoginEvent>().Publish(true);
            Task.Run(() => {
                Queue.ClearAllQueue();
                Queue.Save2Db();
                ReloadQueue();
                Flag = true;
                eventAggregator.GetEvent<LoginEvent>().Publish(false);
            });

        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
                RefresshQueue();

        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
          //  throw new NotImplementedException();
        }
    }
}
